import {Ref, ref} from "vue";
import axios from "@/utils/axios";
import {message} from "ant-design-vue";

class VehicleDeviceInfo{
    deviceId: number|null = null
    driverName: string | undefined = undefined
    identity: string | undefined = undefined
    driverPhone:string | undefined = undefined
    driverTeam:string | undefined = undefined
    speedMax:string | undefined = undefined
    brand:string | undefined = undefined
}

export default ()=>{
    const vehicleDeviceRef = ref()
    const vehicleDeviceViewInfo = ref({
        showModal:false
    })
    const vehicleDeviceInfo = <Ref<VehicleDeviceInfo>>ref(new VehicleDeviceInfo())
    const vehicleDeviceRules = {
        driverName:[{
            required: true,
            message: "请输入驾驶员名称",
            type: 'string',
            trigger: 'change'
        }],
        identity:[{
            required: true,
            message: '请输入身份证号',
            type: 'string',
            trigger: 'change'
        }],
        driverPhone:[{
            required: true,
            message: '请输入联系人电话',
            type: 'string',
            trigger: 'change'
        }],
    }

    /**
     * 获取车辆详细信息
     * @param deviceId 设备id
     * @param callback 回调函数
     */
    const getVehicleDeviceInfo = (deviceId:number, callback: Function|null = null)=>{
        vehicleDeviceInfo.value = new VehicleDeviceInfo();
        axios.post("/beta/findDeviceParameterByDeviceId",{deviceId:deviceId}).then((res:any)=>{
            if(res.success){
                Object.assign(vehicleDeviceInfo.value,res.data)
            }else{
                vehicleDeviceInfo.value.deviceId = deviceId
            }
            if(callback) callback()
        })
    }

    /**
     * 设置车辆设备详细信息
     */
    const setVehicleDeviceInfo = (callback:Function|null=null)=>{
        vehicleDeviceRef.value.validate().then(()=>{
            console.log(vehicleDeviceInfo.value)
            axios.post("/beta/saveDeviceParameterByDeviceId",vehicleDeviceInfo.value).then((res:any)=>{
                if(res.success){
                    message.info("保存成功")
                    if(callback) callback()
                }else{
                    message.info(res.msg)
                }
            })
        }).catch((e:any)=>{
            console.log(e)
        })
    }

    return {
        vehicleDeviceRef,
        vehicleDeviceViewInfo,
        vehicleDeviceInfo,
        vehicleDeviceRules,
        getVehicleDeviceInfo,
        setVehicleDeviceInfo
    }
}